package com.study.service.impl;

import com.study.configs.RedisKeys;
import com.study.dao.MemberEducationMapper;
import com.study.pojo.entity.MemberEducation;
import com.study.pojo.req.MemberEducationReq;
import com.study.pojo.resp.ResultMessage;
import com.study.service.MemberEducationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class MemberEducationServiceImpl implements MemberEducationService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    MemberEducationMapper memberEducationMapper;

    @Override
    public ResultMessage selectAll() {
        //判断redis中是否存在
        Boolean hasKey = redisTemplate.hasKey(RedisKeys.EDUCATION_MEMBER);
        if(!hasKey){
            //从数据库中查询
            List<MemberEducation> memberEducations = memberEducationMapper.selectAll();
            List list = new ArrayList();
            //非空判断
            if(memberEducations!=null && memberEducations.size()>0){
                for(MemberEducation memberEducation:memberEducations){
                    MemberEducationReq memberEducationReq = new MemberEducationReq();
                    memberEducationReq.setId(memberEducation.getId());
                    memberEducationReq.setTittle(memberEducation.getTittle());
                    memberEducationReq.setReleaseTime(memberEducation.getReleaseTime());
                    memberEducationReq.setDepartmentName(memberEducation.getDepartmentinfo().getDepartmentName());
                    System.out.println("memberEducationReq = " + memberEducationReq);
                    list.add(memberEducationReq);
                }
                //存储到redis中
                redisTemplate.opsForList().rightPushAll(RedisKeys.EDUCATION_MEMBER,list);
            }else{
                //存储到redis中
                redisTemplate.opsForList().rightPushAll(RedisKeys.EDUCATION_MEMBER,list);
                //设置失效时间
                redisTemplate.expire(RedisKeys.EDUCATION_MEMBER,60, TimeUnit.SECONDS);
            }
        }
        //从redis中获取
        List range = redisTemplate.opsForList().range(RedisKeys.EDUCATION_MEMBER, 0,-1);
        Long size = redisTemplate.opsForList().size(RedisKeys.EDUCATION_MEMBER);
        return new ResultMessage(1,"OK",range,size);
    }

    @Override
    public ResultMessage queryById(Integer id) {
        Boolean hasKey = redisTemplate.hasKey(RedisKeys.EDUCATION_MEMBER_INFO + id.toString());
        if(!hasKey){
            //从数据库中查询
            MemberEducation memberEducation = memberEducationMapper.queryById(id);
            if(memberEducation!=null){
                //存储到redis中
                redisTemplate.opsForHash().put(RedisKeys.EDUCATION_MEMBER_INFO,id.toString(),memberEducation);
            }else{
                redisTemplate.opsForHash().put(RedisKeys.EDUCATION_MEMBER_INFO,id.toString(),"");
                //设置失效时间
                redisTemplate.expire(RedisKeys.EDUCATION_MEMBER_INFO,60,TimeUnit.SECONDS);
            }
        }
        //从redis中获取
        Object obj = redisTemplate.opsForHash().get(RedisKeys.EDUCATION_MEMBER_INFO, id.toString());
        if(StringUtils.isEmpty(obj.toString())){
            return new ResultMessage().ERROR();
        }
        return new ResultMessage(1,"OK",obj,null);
    }
}
